# apps/accounts/backends.py
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password

# 自定义认证后端通常继承自 django.contrib.auth.backends.ModelBackend 或直接实现了必要的接口方法，如 authenticate() 和 get_user()
class CustomAuthBackend(ModelBackend):
    def authenticate(self, request, identifier=None, password=None, **kwargs):

        UserModel = get_user_model()

        try:
            user = UserModel.objects.get(identifier=identifier)

            if user.check_password(password):
                return user
        except UserModel.DoesNotExist:
            return None

    def get_user(self, user_id):
        UserModel = get_user_model()
        try:
            return UserModel.objects.get(pk=user_id)
        except UserModel.DoesNotExist:
            return None


